package DynamicProgramming;
/*
 * 最长回文子序列
 */
public class lc516 {

    public int longestPalindromeSubseq(String s) {
        char[] S = s.toCharArray();
        int len = S.length;
        int[][] dp = new int[len][len];
        for (int i = 0; i < len; i++) {
            dp[i][i] = 1;
        }
        for (int i = len - 1; i >= 0; i--) {
            for (int j = i + 1; j < len; j++) {
                if (S[i] == S[j]) {
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                } else {
                    dp[i][j] = Math.max(dp[i + 1][j], Math.max(dp[i][j - 1], dp[i][j]));
                }
            }
        }
        return dp[0][len - 1];
    }

}
